function DatumAdd() {
    var _this = this;
    var page;
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initTable();
        _this.initEvent();
    }

    // 附件列表
    _this.initTable = function() {
        var list_url = appPath + "/attachment/attachmentList";
        page.init("attachment_form", list_url, "attachment_list", "attachment_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                // 组装数据
                inner += "<tr>";
                inner += "<td>"+(parseInt(i)+1)+"</td>";
                inner += "<td><input type='checkbox' value='"+item.intId+"'/></td>";
                inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.varLocalName+"</span></td>";
                inner += '<td>';
                inner += '<a href="javascript:datumAdd.preView(\''+item.intId+'\');" class="a a-info">预览</a> ';
                inner += '<a href="javascript:datumAdd.deleteAttachment(\''+item.intId+'\');" class="a a-info">删除</a>';
                inner += '<a href="javascript:datumAdd.download(\''+item.intId+'\');" class="a a-view">下载</a> ';
                inner += "</td>";
                inner += "</tr>";
            }
            return inner;
        }

    };

    _this.initEvent = function () {
        $("#btn_add_attachment").click(function () {
            _this.addFile();
        });
    }
    
    // 保存资料
    _this.save = function () {
        var datumName = $.trim($("#datumName").val());
        if ("" == datumName) {
            layer.msg("请填写资料名称")
            return;
        }
        var datumType = $.trim($("#datumType").val());
        if ("" == datumType) {
            layer.msg("资料类型");
            return;
        }
        $.ajax({
            url: appPath + '/admin/projectOffline/saveDatum',
            async: false,
            type: 'post',
            data: {
                'projectId': $("#projectId").val(), 	// 资料所属项目id
                'datumId': $("#datumId").val(), 	    // 资料id
                'datumName': datumName, 	            // 资料类型
                'datumType': datumType	                // 资料名称
            },
            success: function(data){
                var code = data['code'];
                if(code){
                    layer.alert('保存成功', {icon: 1,  skin: 'layer-ext-moon'}, function(index){
                        layer.close(index);
                        $("#save_able").hide();
                        $("#save_disable").show();
                        $("#add_attachment_btn").show();
                        $("#datumName").attr("readonly",true);
                        $("#datumType").attr("readonly",true);
                    });
                }else{
                    layer.alert('操作失敗', {icon: 2,  skin: 'layer-ext-moon'});
                }
            }
        });
    }

    // 添加附件
    _this.addFile =  function() {
        var datumId = $('#datumId').val();
        layer.open({
            title: "添加附件",
            type: 2,
            area: ["600px", "345px"],
            content: [appPath + '/plupload/common?datumId=' + datumId, 'no'],
            cancel: function (index) {
                //关闭窗口，刷新附件列表
                parent.layer.close(index);
                _this.initTable();
            }
        });
    }

    // 预览
    _this.preView = function (intId) {
        $.ajax({
            url: appPath + "/attachment/isTranscoding",
            data: {
                "intId": intId
            },
            dataType: "json",
            async: true,
            type: "GET",
            success: function (data) {
                var result = eval(data).result;
                if (result == true) {
                    layer_show("附件预览", appPath+"/attachment/view?intId=" + intId, "800", "600");
                } else if (result == false) {
                    //转码失败
                    layer.msg("转码中...", {icon: 7});
                } else if (result == "error") {
                    //接口报错
                    layer.msg("系统异常,请联系管理员!", {icon: 7});
                }
            }
        });
    }

    // 删除
    _this.deleteAttachment = function (intId) {
        layer.confirm("确定此操作吗?", {
            icon : 3,
            btn : [ "确认", "取消" ]
        }, function() {
            $.ajax({
                url: appPath + '/attachment/deleteAttachment',
                async: false,
                type: 'post',
                data: {
                    'datumId':$("#datumId").val(),
                    'intId': intId	  // 要删除的附件id
                },
                success: function(data){
                    var code = data['code'];
                    if(code){
                        layer.msg('操作成功', {icon: 1});
                        _this.initTable();
                    }else{
                        layer.msg('操作失敗', {icon: 2});
                    }
                }
            });
        });
    }

    // 下载单个文件
    _this.download = function (intId) {
        location.href = appPath + '/exportAttachment/exportAttachment.do?&id=' + intId;
    }
    
    _this.closeWindow = function () {
        // 刷新父页面
        window.opener.location.reload();
        // 关闭页面
        window.close();
    }


}

var datumAdd = new DatumAdd();